package com.syl.qianyin.spider.service;

import com.syl.framework.frametools.utils.RedisObjectUtils;
import com.syl.qianyin.spider.beans.AvMooBean;
import com.syl.qianyin.spider.constants.RedisKeyConstant;
import com.syl.qianyin.spider.exend.DynamicProxyProvider;
import com.syl.qianyin.spider.exend.CustomHttpClientDownloader;
import com.syl.qianyin.spider.exend.interfaces.CustomPageProcessor;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.selector.Html;
import java.util.List;
import java.util.ListIterator;

/**
 * avmoo 爬虫
 * @author syl
 * @create 2019-05-10 16:53
 **/
@Service
public class AvMooSpiderService implements CustomPageProcessor {
    private static final Logger LOG = LoggerFactory.getLogger(AvMooSpiderService.class);
    String targetUrl = "https://javzoo.com/cn";

    @Autowired
    private RedisObjectUtils redisObjectUtils;
    @Autowired
    private DynamicProxyProvider dynamicProxyProvider;

    @Override
    public void myProcess(Page page, String url, Html html, Document document) {
        String baseMatchUrl = "https://\\w+.\\w+/cn";
        // 引导页面
        if(
            page.getUrl().regex(baseMatchUrl).match() ||
                page.getUrl().regex(baseMatchUrl + "/released").match() ||
                page.getUrl().regex(baseMatchUrl + "/popular").match() ||
                page.getUrl().regex(baseMatchUrl + "/genre/\\w+").match()
        ){
            LOG.debug("匹配到引导页面 {}", url);
            initPage(page, document);
        }
        if(page.getUrl().regex(baseMatchUrl + "/movie/\\w+").match()){
            LOG.debug("匹配到引导页面 {}", url);
            detailPage(page, url, html, document);
        }
    }

    /**
     * 设置引导页数据
     * @param page
     */
    private void initPage(Page page, Document document){
        ListIterator<Element> iterator = document.select("#waterfall .item").listIterator();
        int i = 1;
        while (iterator.hasNext()){
            Element next = iterator.next();
            String title = next.select("img").attr("title");
            String image = next.select("img").attr("src");
            String detailUrl = next.select("a").attr("href");
            AvMooBean avMooBean = new AvMooBean(title, image, detailUrl);
            LOG.debug("引导页 第几个{}item  数据{}", i, avMooBean);
            redisObjectUtils.putMapCache(RedisKeyConstant.MAP_MOO_DATA_KEY, detailUrl, avMooBean);
            page.addTargetRequest(detailUrl);// 深度挖取详情页数据
            i ++;
        }
    }

    private void detailPage(Page page, String url, Html html, Document document){
        AvMooBean avMooBean = redisObjectUtils.getMapCache(RedisKeyConstant.MAP_MOO_DATA_KEY, url, AvMooBean.class);
        String goodId = splitSelect(html, ".movie .info p:eq(1)");
        String releaseTime = splitSelect(html, ".movie .info p:eq(2)");
        String length = splitSelect(html, ".movie .info p:eq(3)");
        String director = splitSelect(html, ".movie .info p:eq(4)");
        String makeMerchant = document.select(".movie .info p:eq(6)").text();
        String releaseMerchant = document.select(".movie .info p:eq(8)").text();
        List<String> typeList = document.select(".movie .info p:eq(10) span").eachText();

        if(hasSelect(html, "#avatar-waterfall")) {
            List<String> actressList = document.select("#avatar-waterfall .avatar-box span").eachText();
            List<String> actressImageList = document.select("#avatar-waterfall .avatar-box img").eachAttr("src");
            avMooBean.setActressList(actressList).setActressImageList(actressImageList);
        }
        if(hasSelect(html, "#sample-waterfall")) {
            List<String> previewImageList = document.select("#sample-waterfall .photo-frame img").eachAttr("src");
            avMooBean.setPreviewImageList(previewImageList);
        }
        avMooBean.setDetailUrl(url)
            .setGoodId(goodId).setReleaseTime(releaseTime).setLength(length)
            .setDirector(director).setMakeMerchant(makeMerchant)
            .setReleaseMerchant(releaseMerchant).setTypeList(typeList);
        LOG.debug("成功抓取详情页  汇总数据 {}", avMooBean);
        page.putField("index_" + url, avMooBean);
    }

    private boolean hasSelect(Html html, String select){
        return StringUtils.isNotEmpty(html.$(select).get());
    }

    private String splitSelect(Html html, String select){
        return splitSelect(html, select, " ", 1);
    }

    private String splitSelect(Html html, String select, String split, int index){
        if(!hasSelect(html, select))
            return null;
        String[] temp = html.getDocument().select(select).text().split(split);
        if(temp.length <= index)
            return null;
        return temp[index];
    }

    @Override
    public void start() {
        Spider.create(this)
            .addUrl(targetUrl)
            .setDownloader(new CustomHttpClientDownloader(dynamicProxyProvider))
            .thread(5)
            .run();
    }

}
